package org.com.system.log;

import org.com.system.util.utils.DateUtils;
import org.com.system.util.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

@Repository("Logger")
@Scope("singleton")
public class Logger {

	public static int UNDEFINED_OPERATION=-1;//未定义操作类型
	public static int INSERT_OPERATION=0;//新增操作类型
	public static int DELETE_OPERATION=1;//删除操作类型
	public static int SELECT_OPERATION=2;//查询操作类型
	public static int UPDATE_OPERATION=3;//更新操作类型
	public static int SERVICE_OPERATAION=4;//服务操作类型
	public static int CUSTOM_OPERATION=5;//自定义操作类型
	
	
	@Autowired(required=true)
	@Qualifier("logServer")
	private LogService logServer;
	private static Log log = getLog();
	
	private static Logger logger=new Logger();
	
	public static Logger getLogger(){
		if(null==logger){
			logger=new Logger();
		}
		return logger;
	}
	
    /**
     * 记录自定义操作	
     * @param operation
     */
	public void info(String operation){
		Log log=getLog();
		log.setInfo("", "",Logger.CUSTOM_OPERATION, operation);
		logServer.save(log);
	}
	
	/**
	 * 记录新增操作
	 * @param operation
	 */
    public void insertOperationLog(String operation){
    	Log log=getLog();
		log.setInfo("uname", "user_ip",Logger.INSERT_OPERATION, operation);
		logServer.save(log);
	}
    
    /**
     * 记录删除操作
     * @param operation
     */
    public void deleteOperationLog(String operation){
    	Log log=getLog();
		log.setInfo("", "",Logger.DELETE_OPERATION, operation);
		logServer.save(log);
	}
	
    /**
     * 记录查询操作
     * @param operation
     */
    public void selectOperationLog(String operation){
    	Log log=getLog();
		log.setInfo("", "",Logger.SELECT_OPERATION, operation);
		logServer.save(log);
    }
    
    /**
     * 记录更新操作
     * @param operation
     */
    public void updateOperationLog(String operation){
    	Log log=getLog();
		log.setInfo("", "",Logger.UPDATE_OPERATION, operation);
		logServer.save(log);
    }
    
    /**
     * 记录服务操作
     * @param operation
     */
    public void serviceOperationLog(String operation){
    	Log log=getLog();
		log.setInfo("", "",Logger.SERVICE_OPERATAION, operation);
		logServer.save(log);
    }
    
    /**
     * 取得操作名称
     * @param operType
     * @return
     */
    public static String getOperationName(int operType){
    	String name=null;
    	switch(operType){
    	    case -1:name="未定义操作";
    	    case 0:name="新增操作";
    	    case 1:name="删除操作";
    	    case 2:name="查询操作";
    	    case 3:name="更新操作";
    	    case 4:name="服务操作";
    	    case 5:name="自定义操作";
    	}
    	return name;
    }
    
    public static Log getLog(){
		if(null==log){
			log=Log.getLog();
		}else{
			log.setId(UUIDUtils.getUUID());
			log.setCreateTime(DateUtils.getSystime());
		}
		return log;
	}
    
}
